package br.com.trimark.business;

import br.com.caelum.vraptor.ioc.Component;

import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import br.com.trimark.pojo.Usuario;

@Component
public class UsuarioBusiness {
	
	private EntityManager manager;
	
	public UsuarioBusiness(EntityManager manager) {
		this.manager = manager;
	}
	
	public Usuario autenticar(String login, String senha) {
		try {
			Query query = manager.createQuery("from Usuario where login = :login and senha = :senha");
			query.setParameter("login", login);
			query.setParameter("senha", senha);
			
			return (Usuario) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		
	}
	
}
